/**
 * Created by mj on 2017/3/30.
 */
$(function () {
    $('#dglist').datagrid({
        url: "productionprojectqueryWithPaging",
        method: "post",
        title: "工艺流程卡列表",
        striped: true,
        singleSelect: true,
        rownumbers: true,
        loadMsg: "努力加载中...",
        fit: true,
        fitColumns: true,//是否自动填充适合屏幕
        pagination: true,//是否显示底部分页工具栏
        pageSize: 15,
        pageList: [15, 20, 25],
        showFooter: true,
        autoRowHeight: false,
        columns: [[
            {field: 'id', hidden: 'true'},
            {field: 'projectName', title: '项目名称', sortable: true, width: 100, align: "center"},
            {field: 'partsName', title: '零件名称', sortable: true, width: 100, align: "center"},
            {field: 'chartNumber', title: '图号', sortable: true, width: 100, align: "center"},
            {field: 'oderNumber', title: '订单数量', sortable: true, width: 100, align: "center"},
            {field: 'status', title: '状态', sortable: true, width: 100, align: "center"}
        ]],
        toolbar: [
            {
                text: '增加', iconCls: 'icon-add', handler: function () {
                openAddTechnologyWin();
            }
            }, '-',
            {
                text: '修改基本信息', iconCls: 'icon-edit', handler: function () {
                editProject();
            }
            }, '-',
            {
                text: '修改项目工艺', iconCls: 'icon-edit', handler: function () {
                openEditTechnologyListWin();
            }
            }, '-',
            {
                text: '删除', iconCls: 'icon-remove', handler: function () {
                deleteProject();
            }
            }, '-',
            {
                text: '查看', iconCls: 'icon-more', handler: function () {
                opendetailWin();
            }
            }, '-',
            {
                text: '刷新', iconCls: 'icon-reload', handler: function () {
                $('#dglist').datagrid("reload");
            }
            }, '-',
            {
                text: '启动/暂停', iconCls: 'icon-lock', handler: function () {
                changeStatus();
            }
            },
            '-'],
    });
    $('#dglist').datagrid('getPager').pagination({//分页栏下方文字显示
        beforePageText: '第',
        afterPageText: '页，总共 {pages}页',
        displayMsg: '当前显示从第{from}条到{to}条 共{total}条记录',
    });
    $('#dglist').datagrid({
        rowStyler: function (index, row) {
            if (row.status == '正常') return 'background-color:green;';
            if (row.status == '返修') return 'background-color:pink;';
            if (row.status == 'NG') return 'background-color:red;';
            if (row.status == '暂停') return 'background-color:yellow;';
        }
    });
});


// function save(listId, winId, formId, flag) {
//
//     if ($("#" + formId).form("validate")) {
//         var form = $("#" + formId)[0];
//         var formData = new FormData(form);
//         $.messager.confirm("提醒", "您确认操作吗？", function (r) {
//             if (r) {
//                 $.ajax({
//                     url: "add",
//                     data: formData,
//                     type: "post",
//                     dataType: "json",
//                     async: false,
//                     cache: false,
//                     contentType: false,
//                     processData: false,
//                     success: function (data) {
//
//
//
//                         if (data.msg == "success") {
//                             $.messager.alert("提示", "操作成功！", "info", function () {
//                                 $("#" + listId).datagrid('reload');
//                                 $("#" + winId).window("close");
//                                 $("#" + formId).form("reset");
//                                 $("#editJisForm").form("reset");
//                             });
//                         } else {
//                             $.messager.alert("警告", "操作失败！", "error", function () {
//                                 window.location.reload();
//                             })
//                         }
//                     },
//                     error: function (data) {
//                         //alert("s")
//                     },
//                 })
//             }
//         })
//     }
// }
//删除，可批量删除
function deleteUser() {
    var ids = $.getManyRecords("dglist")
    $.messager.confirm('确认', '您确认想要删除记录吗？', function (r) {
        if (r) {
            $.ajax({
                type: "POST",
                url: "delete",
                data: {ids: ids},
                timeout: 20000,
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data.msg == "success") {
                        $.messager.alert("提示", "删除成功！", "info", function () {
                            window.location.reload();
                        })
                    } else {
                        $.messager.alert("提示", "删除失败！", "info", function () {
                            window.location.reload();
                        });
                    }

                },
                error: function () {
                    $.messager.alert("提示", "出现了不可预知的错误，请重新操作！");
                }
            })
        }
    });
}


//删除项目

function deleteProject() {
    var rows = $("#dglist").datagrid("getChecked");
    var ids = $.getManyRecords("dglist")
    var DNs = "";
    for (var i = 0; i < rows.length; i++) {
        DNs = DNs + rows[i].chartNumber + ",";
    }

    $.messager.confirm('确认', '您确认想要删除记录吗？', function (r) {
        $.ajax({
            type: "GET",
            url: "deleteProject",
            data: {ids: ids, DNs: DNs},
            timeout: 20000,
            dataType: "json",
            cache: false,
            success: function (data) {
                if (data.msg == "success") {
                    $.messager.alert("提示", "删除成功！", "info", function () {
                        window.location.reload();
                    });
                } else {
                    $.messager.alert("提示", "删除失败！", "info", function () {
                        window.location.reload();
                    });
                }

            },
            error: function () {
                $.messager.alert("提示", "出现了不可预知的错误，请重新操作！");
            }
        })
    })
}

//打开修改工艺信息的窗口
function openEditTechnologyListWin() {
    if (!$.getSingleRecord("dglist")) {
        return;
    }
    var row = $.getSingleRecord("dglist");
    $("#showProjectId").val(row.id);
    $("#showProjectName").textbox("setValue", row.projectName);
    $('#showProjectName').textbox('textbox').attr('disabled', true);
    $("#showPartsName").textbox("setValue", row.partsName);
    $('#showPartsName').textbox('textbox').attr('disabled', true);
    $("#showChartNumber").textbox("setValue", row.chartNumber);
    $('#showChartNumber').textbox('textbox').attr('disabled', true);
    $("#showOderNumber").textbox("setValue", row.oderNumber);
    $('#showOderNumber').textbox('textbox').attr('disabled', true);
    $('#technologydglist').datagrid({url: 'technologicalprocessqueryWithPaging'});
    $('#technologydglist').datagrid('load', {project_id: row.id});
    if ($("#updateTechnology").length <= 0) {
        $("#btnpanel").append("<a id='updateTechnology'  iconCls='icon-edit' onclick='openEditTechnologyWin()'  class='easyui-linkbutton'>修改</a>")
        $.parser.parse("#btnpanel");
    }

    $('#addTechnologyWin').show().window('open');


}

//确认修改工艺
function editTechnology() {
    $('#editTechnologyForm').form('submit', {
        onSubmit: function () {
            if (!$(this).form('enableValidation').form('validate')) {
                return $(this).form('enableValidation').form('validate');
            }
            $.ajax({
                type: "POST",
                url: "updateTechnologicalProcess",
                data: $("#editTechnologyForm").serialize(),
                dataType: "json",
                success: function (data) {
                    if (data.msg == "success") {
                        $.messager.alert("提示", "修改成功！", "info", function () {
                            cancelWin('editTechnologyWin', 'editTechnologyForm');
                            $('#technologydglist').datagrid('load', {project_id: $.getSingleRecord("dglist").id});
                        })
                    } else {
                        $.messager.alert("提示", "修改失败！", "info", function () {
                            $('#technologydglist').datagrid('load', {project_id: $.getSingleRecord("dglist").id});
                        })
                    }
                },
                error: function () {
                    $.messager.alert("提示", "出现了不可预知的错误，请刷新重试！");
                }
            });
        }
    })
}

//打开修改工艺信息的窗口
function openEditTechnologyWin() {
    if (!$.getSingleRecord("technologydglist")) {
        return;
    }
    var id = $.getSingleRecord("technologydglist").id;
    $.ajax({
        type: "Post",
        url: "toUpdateTechnology",
        data: {id: id},
        timeout: 20000,
        dataType: "json",
        cache: false,
        success: function (data) {
            if (data.msg == "success") {
                // $("#tpId").val(data.tp.id);
                $("#tpId").textbox("setValue", data.tp.id);
                $("#projectId").textbox("setValue", $.getSingleRecord("dglist").id);

                $("#tpProductionProcesses").textbox("setValue", data.tp.productionProcesses);
                $("#tpSequenceNumber").textbox("setValue", data.tp.sequenceNumber);
                $("#tpProcessContent").textbox("setValue", data.tp.processContent);
                $("#tpProductionQuantity").textbox("setValue", data.tp.productionQuantity);
                $("#tpEstimateTime").textbox("setValue", data.tp.estimateTime);
                $("#tpOperator").textbox("setValue", data.tp.operator);
                $("#tpId").next().hide();
                $("#projectId").next().hide();
                $("#editTechnologyWin").show().window("open");

            } else {
                $.messager.alert("提示", "获取本条记录信息失败失败！", "info", function () {
                    window.location.reload();
                });
            }

        },
        error: function () {
            $.messager.alert("提示", "出现了不可预知的错误，请重新操作！");
        }
    })
}


//修改项目信息
function editProject() {
    if (!$.getSingleRecord("dglist")) {
        return;
    }
    var id = $.getSingleRecord("dglist").id;
    $.ajax({
        type: "GET",
        url: "toUpdate",
        data: {id: id},
        timeout: 20000,
        dataType: "json",
        cache: false,
        success: function (data) {
            if (data.msg == "success") {
                // alert(data.project.id);
                $("#id").textbox("setValue", data.project.id);
                $("#projectName").textbox("setValue", data.project.projectName);
                $("#partsName").textbox("setValue", data.project.partsName);
                $("#chartNumber").textbox("setValue", data.project.chartNumber);
                $("#oderNumber").textbox("setValue", data.project.oderNumber);
                openWin("dglist", "updateProjectWin", "修改项目基本信息", "450px", "300px", "updateProjectForm", "update");
                $("#id").next().hide();
                $("#updateProjectWin").show().window("open");
            } else {
                $.messager.alert("提示", "获取本条记录信息失败失败！", "info", function () {
                    window.location.reload();
                });
            }

        },
        error: function () {
            $.messager.alert("提示", "出现了不可预知的错误，请重新操作！");
        }
    })
}

/**
 * 改变用户状态
 */
function changeStatus() {
    if (!$.getSingleRecord("dglist")) {
        return;
    }
    var id = $.getSingleRecord("dglist").id;
    $.messager.confirm('确认', '您确认要改变该工序的状态吗？', function (r) {
        if (r) {
            $.ajax({
                type: "POST",
                url: "changeStatus",
                data: {id: id},
                timeout: 20000,
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data.msg == "success") {
                        $.messager.alert("提示", "状态改变成功！", "info", function () {
                            $('#dglist').datagrid("reload");
                        })
                    } else {
                        $.messager.alert("提示", "操作失败！", "error", function () {
                            $('#dglist').datagrid("reload");
                        })
                    }
                }
            })
        }
    });
}

function opendetailWin() {
    $("#detailWin").show().window("open");
}

function opendetailWin() {
    // $("#editRoleForm").form("reset");
    if (!$.getSingleRecord("dglist")) {
        $('#detailWin').window('close');
        return;
    }
    $('#detailWin').window('open');
    var row = $.getSingleRecord("dglist");
    $('#detailList').datagrid({
        url: "technologicalprocessqueryWithPaging",
        method: "post",
        striped: true,
        loadMsg: "努力加载中...",
        queryParams: {project_id: row.id},
        fitColumns: true,//是否自动填充适合屏幕
        autoRowHeight: false,
        columns: [[
            {field: 'sequenceNumber', title: '序号', sortable: true, align: "center"},
            {field: 'productionProcesses', title: '工序', sortable: true, align: "center"},
            {field: 'processContent', title: '工艺内容', sortable: true, align: "center"},
            {field: 'productionQuantity', title: '生产数量', sortable: true, align: "center"},
            {
                field: 'estimateTime', title: '预估工时', sortable: true, align: "center",
                formatter: function (value, row, index) {
                    return (row["estimateTime"]).toFixed(2);
                }
            },
            {field: 'operator', title: '操作员', sortable: true, width: 100, align: "center"},
            {field: 'receptionConfirmation', title: '下工段接收确认', sortable: true, width: 100, align: "center"},
            {field: 'remarks', title: '备注', sortable: true, width: 100, align: "center"}
        ]],
    });
}
